<?php
namespace core;
use core\lib\Log;
class Loader{
	//定义静态成员属性
	public static $classMap = array();
	
	//定义静态成员方法
	public static function run(){
		//写入日志
		/* Log::init();
		Log::setLog('dddd---'); */
		//自动加载文件
		spl_autoload_register('self::load');
		
		$route = new \core\lib\route;
		$ctrlClass = $route->ctrl;
		$action = $route->action;
		
		$ctrlFile = APP.'/Controller/'.$ctrlClass.'.php';
		$cltrClass = '\\'.MODULE.'\\'.$ctrlClass;
		
		if(is_file($ctrlFile)){
			include($ctrlFile);
			$ctrl = new $cltrClass;
			$ctrl->$action();
		}else{
			throw new \Exception('找不到控制器'.$ctrlClass);
		}
	}
	//自动加载文件
	public static function load($class){
		if(isset($classMap[$class])){
			//var_dump($classMap);
			return true;
		}else{
			$class = str_replace('\\','/',$class);
			$file = HYYPATH.'/'.$class.'.php';
			if(is_file($file)){
				include($file);
				self::$classMap[$class] = $class;
			}else{
				return false;
			}
		}
	}
	//模板赋值
	public function assign($name,$value){
		$this->assign[$name] = $value;
	}
	//引用模板
	public function fetch($file){
		$file = APP.'/View/'.$file;
		if(is_file($file)){
			extract($this->assign);
			include $file;
		}
	}
}